package com.anjoyo.xgh.musicshare.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.anjoyo.xgh.musicshare.entry.ShareBean;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.entry.User;

public class JsonUtil {

	/**
	 * { "code":200, "user":{ "date":"1370662929033",
	 * "descrption":"����Ա���Ǻ�����", "id":149,
	 * "img_icon":"userimage/wangqiong/head.png", "name":"wangqiong",
	 * "nickname":"������"} ,"msg":"��½�ɹ�"}
	 * 
	 * @param json
	 * @return
	 */
	public static User paseUser(String json) {
		if (json == null)
			return null;
		User user = null;
		try {
			JSONObject OBJ = new JSONObject(json);
			JSONObject jsonObject = OBJ.getJSONObject("user");
			user = new User();
			user.setId(jsonObject.getLong("id"));
			user.setName(jsonObject.getString("name"));
			user.setNickname(jsonObject.getString("nickname"));
			user.setDescrption(jsonObject.getString("descrption"));
			user.setImg_icon(jsonObject.getString("img_icon"));
			user.setDate(jsonObject.getString("date"));
			user.setSmall_icon(jsonObject.getString("small_icon"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return user;
	}

	public static User paseOauthUser(String json) {
		if (json == null)
			return null;
		User user = null;
		try {
			JSONObject object = new JSONObject(json);
			user = new User();
			user.setId(object.getLong("id"));
			user.setImg_icon(object.getString("avatar_large"));
			user.setDescrption(object.getString("description"));
			user.setSex(object.getString("gender"));
			user.setNickname(object.getString("screen_name"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return user;
	}

	public static ArrayList<Song> ParseNetSong(String json) {
		ArrayList<Song> songs = new ArrayList<Song>();
		if (json == null) {
			return null;
		}
		try {
			JSONArray array = new JSONArray(json);
			Song song = null;
			for (int i = 0; i < array.length(); i++) {
				song = new Song();
				JSONObject js = array.getJSONObject(i);
				song.setMusicid(js.getInt("track_id"));
				song.setName(js.getString("music_name"));
				song.setArtist(js.getString("music_artist"));
				song.setAblum(js.getString("music_ablum"));
				song.setLocal_url("");
				song.setNet_url(js.getString("music_url"));
				song.setLrc_url(js.getString("lrc_url"));
				song.setImage_url(js.getString("image_url"));
				song.setIsnetMusic(true);
				song.setIsdoffline(false);
				songs.add(song);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return songs;
	}

	public static ShareBean parseShare(JSONObject jsonObject) {
		ShareBean shareBean = new ShareBean();
		try {
			shareBean.setAddress(jsonObject.getString("address"));
			shareBean.setContent(jsonObject.getString("content"));
			shareBean.setFile(jsonObject.getString("file"));
			shareBean.setId(jsonObject.getLong("id"));
			shareBean.setImg_lager(jsonObject.getString("img_lager"));
			shareBean.setImg_small(jsonObject.getString("img_small"));
			shareBean.setLat(jsonObject.getString("lat"));
			shareBean.setLon(jsonObject.getString("lon"));
			shareBean.setLikecount(jsonObject.getInt("likecount"));
			shareBean.setIslike(jsonObject.getBoolean("islike"));
			shareBean.setUserid(jsonObject.getString("userid"));
			shareBean.setMuisicid(jsonObject.getInt("muisicid"));
			shareBean.setMusicurl(jsonObject.getString("musicurl"));
			shareBean.setTime(jsonObject.getString("time"));
			shareBean.setAudiotime(jsonObject.getString("audiofile_duration"));
			if (!jsonObject.isNull("user")) {
				JSONObject js = new JSONObject(jsonObject.getString("user"));
				if (js != null) {
					User user = new User();
					user.setId(js.getLong("id"));
					user.setName(js.getString("name"));
					user.setNickname(js.getString("nickname"));
					user.setDescrption(js.getString("descrption"));
					user.setImg_icon(js.getString("img_icon"));
					user.setDate(js.getString("date"));
					user.setSmall_icon(js.getString("small_icon"));
					user.setType(js.getString("type"));
//					System.out.println("打印user" + user.toString());
					shareBean.setUser(user);
				}
			}
			if (!jsonObject.isNull("music")) {
				JSONObject js = new JSONObject(jsonObject.getString("music"));
				if (js != null) {
					Song song = new Song();
					song.setMusicid(js.getInt("track_id"));
					song.setName(js.getString("music_name"));
					song.setArtist(js.getString("music_artist"));
					song.setAblum(js.getString("music_ablum"));
					song.setLocal_url("");
					song.setNet_url(js.getString("music_url"));
					song.setLrc_url(js.getString("lrc_url"));
					song.setImage_url(js.getString("image_url"));
					song.setIsnetMusic(true);
					song.setIsdoffline(false);
//					System.out.println("打印歌曲song:" + song.toString());
					shareBean.setMusic(song);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return shareBean;
	}

	public static ArrayList<ShareBean> parseShareList(String data) {
		ArrayList<ShareBean> shareBeans = new ArrayList<ShareBean>();
		try {
			JSONArray array = new JSONArray(data);
			for (int i = 0; i < array.length(); i++) {
				shareBeans.add(parseShare(array.getJSONObject(i)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return shareBeans;
	}

}
